#include <iostream.h>

来源:百度知道 编辑:UC知道 时间:2024/05/06 18:38:43
#include <iostream.h>
#include <math.h>
void main()
{
double radius,height;
cout <<"请输入圆柱体的半径和高: \n";
cin >>radius >> height;
double volume=radius * radius * height *'M_PI';
cout <<"该圆柱体的体积为:"<<volume<<endl;
cout<<"\n请输入球半径:\n";
cin >> radius;
double areaofsphere=4 * radius * radius *M_PI;
cout <<"该球面面积为:"<<areaofsphere<<endl;
double length,width;
cout<<"\n请输入长方体的长.宽.高;\n";
cin >>length>>width>>height;
volume=length * width * height;
cout <<"该长方体的体积为:"<<volume<<endl;
}
运行结果:k.cpp(12) : error C2065: 'M_PI' : undeclared identifier

原因是你没有定义M_PI,在<iostream>里是没有M_PI的定义的,因为M_PI我们用的是个常量,3.1415927,所以我们可以把他声明为全局变量,就是把 const double M_PI = 3.1415926这句话放在#include <math.h>后面,或者用宏来定义M_PI,就是#define M_PI 3.1415927的语句放在#include <math.h>后面,不过用宏来定义运行效率虽高,但是用宏还是值得注意哦,自己看看用哪个吧

很显然你没有对M_PI定义啊
在main开始写:
const double M_PI=3.14145927;

fgd